<?php

/**
 * 自动生成 WxEventCupidUserService
 * 
 * Apps/Common/Service/WxEventCupidUserService.class.php
 * Zix @ 2015-08-12
 */

namespace Common\Service;

class WxEventCupidUserService {

  private static $instance;

  var $award_status = array(
    1 => '未领取',
    2 => '已领取'
  );
  
  public static function instance() {
    if (self::$instance == null) {
      $instance = new WxEventCupidUserService();
    }

    return $instance;
  }

  function get_default_row() {
    return array(
      'id' => '',
      'event_type' => '',
      'event_id' => '',
      'user_id' => '',
      'award_id' => '',
      'award_text' => '',
      'point' => '',
      'award_status' => '1',
      'award_check_time' => '',
      'ipaddress' => '',
      'create_time' => '',
    );
  }

  function get_by_id($id , $with_beauty = FALSE ) {
    $WxEventCupidUser = M('WxEventCupidUser');
    if ( !$with_beauty ) {
      $data = $WxEventCupidUser->find($id);
    } else {
      $data = $WxEventCupidUser
        ->alias('ecu')
        ->field('ecu.* , ecb.name beauty_name , ecb.true_name beauty_true_name , ecb.icon beauty_icon')
        ->join('wx_event_cupid_beauty ecb on ecb.id = ecu.beauty_id')
        ->where('ecu.id = %d' , $id )
        ->find();
    }
    return $data ? $data : array();
  }

  function get_by_cond($config) {
    $default = array(
      'page' => 1,
      'page_size' => 6,
      'event_type' => '',
      'event_id' => '',
      'user_id' => '',
      'point_gt' => 0 ,
      'count' => FALSE,
      'order' => 'DESC',
      'sort' => 'id',
    );

    $config = extend($config, $default);

    $WxEventCupidUser = M('WxEventCupidUser');

    $where = array();
    
    if (!empty($config['event_id'])) {
      $where['ecu.event_id'] = $config['event_id'];
    }
    
    if (!empty($config['event_type'])) {
      $where['ecu.event_type'] = $config['event_type'];
    }
    
    if (!empty($config['user_id'])) {
      $where['ecu.user_id'] = $config['user_id'];
    }
    
    if (!empty($config['point_gt'])) {
      $where['ecu.point'] = array('EGT' , $config['point_gt']);
    }

    if ($config['count']) {
      return $WxEventCupidUser->alias('ecu')->where($where)->count();
    } else {
      $order = 'ecu.' . $config['sort'] . ' ' . $config['order'];
      $limit = ($config['page'] - 1 ) * $config['page_size'] . ' , ' . $config['page_size'];
      $data = $WxEventCupidUser
        ->alias('ecu')
        ->field('ecu.* , u.truename , u.phone , u.username , u.nickname , ecb.name beauty_name , ecb.true_name beauty_true_name')
        ->join('mer_user u on u.id = ecu.user_id' , 'left')
        ->join('wx_event_cupid_beauty ecb on ecb.id = ecu.beauty_id' , 'left')
        ->where($where)
        ->limit($limit)->order($order)->select();
      //echo $WxEventCupidUser->_sql();
      return $data ? $data : array();
    }
  }

  function create($data, $is_ajax = true) {
    $WxEventCupidUser = M('WxEventCupidUser');
    $ret = $WxEventCupidUser->add($data);
    if ($is_ajax) {
      if ($ret) {
        return ajax_arr('添加成功', TRUE, array(
          'id' => $ret
        ));
      } else {
        return ajax_arr('添加失败', FALSE);
      }
    } else {
      if ($ret) {
        return $ret;
      } else {
        return 0;
      }
    }
  }

  function update($id, $data) {
    $WxEventCupidUser = M('WxEventCupidUser');

    $ret = $WxEventCupidUser->where("id = %d", $id)->save($data);
    if ($ret) {
      return ajax_arr('编辑成功', TRUE);
    } else {
      return ajax_arr('编辑失败', FALSE);
    }
  }

  function delete($ids) {
    $WxEventCupidUser = M('WxEventCupidUser');
    $ret = $WxEventCupidUser->delete($ids);
    if ($ret == 0) {
      return ajax_arr('未删除任何数据', FALSE);
    } else if (!$ret) {
      return ajax_arr('删除失败', FALSE);
    } else {
      return ajax_arr('删除' . $ret . '行数据', TRUE);
    }
  }
  
  function get_total_by_event_user( $event_id , $user_id , $today = TRUE ) {
    $WxEventCupidUser = M('WxEventCupidUser');
    
    $where = array(
      'event_id' => $event_id ,
      'user_id' => $user_id ,
    );

    if ( $today ) {
      $where['create_time'][] = array('gt' , date('Y-m-d 00:00:00'));
      $where['create_time'][] = array('lt' , date('Y-m-d 23:59:00'));
    }

    return $WxEventCupidUser->where( $where)->count();
  }
  
  
  function get_by_user( $user_id , $award_status = '' , $point_gt = 0 ) {
    $WxEventCupidUser = M('WxEventCupidUser');
    
    if ( !empty( $award_status ) ) {
      $where['ecu.award_status'] = $award_status ;
    }  
    
    if ( $point_gt > 0 ) {
      $where['ecu.point'] = array('EGT' , $point_gt ); 
    }
    $where['ecu.user_id'] = $user_id ;
    
    $data = $WxEventCupidUser
      ->alias('ecu')
      ->field('ecu.* , u.username , u.truename , u.phone')
      ->join('mer_user u ON u.id = ecu.user_id' , 'left')
      ->where( $where )
      ->select();
    
    return $data ? $data : array();
  }
  
  function count_user( $event_id ) {
    $WxEventCupidUser = M('WxEventCupidUser');
    
    $where['event_id'] = $event_id;
    
    $count = $WxEventCupidUser->where($where)->count('DISTINCT user_id');
    //echo $WxEventCupidUser->_sql();
    return $count;
  }
  
  /**
   * 统计 用户中奖数
   * @return type
   */
  function get_user_award_total() {
    $WxEventCupidUser = M('WxEventCupidUser');
    
    $field = 'u.ext_id as uid , u.nickname , u.truename , u.phone , count(user_id) as award_amount ,';
    $field .= 'COUNT( IF(award_status = 1 , 1 , NULL) ) AS untake_amount,';
    $field .= 'COUNT( IF(award_status = 2 , 1 , NULL) ) AS taked_amount ,';
    $field .= 'GROUP_CONCAT( ecu.create_time ) AS award_time';
    $data = $WxEventCupidUser
      ->alias('ecu')
      ->field($field )
      ->join('mer_user u on u.id = ecu.user_id' , 'left')
      ->where('ecu.point >= 90 ')
      ->group('ecu.user_id')
      ->select();
    //echo $WxEventCupidUser->_sql();
    return $data ? $data : array();
  }
  
  /**
   * 统计 用户中奖明细
   * @return type
   */
  function get_user_award() {
    $WxEventCupidUser = M('WxEventCupidUser');
    
    $data = $WxEventCupidUser
      ->alias('ecu')
      ->field('u.ext_id as uid  , u.nickname , u.truename , u.phone , （IF(ecu.award_status = 1 , "未领取" , "已领取") ） as award_status_text , ecu.create_time')
      ->join('mer_user u on u.id = ecu.user_id' , 'left')
      ->where('ecu.point >= 90  ')
      ->select();
    return $data ? $data : array();
  }
  
  
  

}
